<?php
/**
* +----------------------------------------------------------------------+
* | $Id: add.php 37 2005-04-03 05:54:36Z vluther $ 												                 | 
* +----------------------------------------------------------------------+
* | Copyright (c) 2000-2005 Linuxpowered, Inc                            |
* +----------------------------------------------------------------------+
* | Author: Vidyut Luther <vid@linuxpowered.com>                         |
* +----------------------------------------------------------------------+
* 
* $URL: http://svn.linuxpowered.com/binder/add.php $
**/

require_once('./global.inc.php') ; 
require_once(INCLUDE_PATH."/Records.class.php"); 

/* Do Some sanity checking here.. 
	1. Figure out how many records we're adding
	2. Go through each record and make sure it follows rules for it's type.
		2a. If Record = MX , make sure there is a priority given. If none given, 
			default to 100. If Multiple MX records are given keep lowering priority by 100.
		2b. If it's an A or CNAME or NS record, make sure the hostname ONLY is provided, AND 
			that the hostname is not the following : www,ftp,stats,mail,mailadmin,webmail. If it's
			any of them, skip the record and say they need to go Edit the domain and not use the Add Feature.

			
*/
/* End Generic Header Block */


$fqdnid = $_POST['mydomain']; 
$rtype = $_POST['rtypes']; 
$priority = $_POST['priority']; 
$hostname = $_POST['hostname']; 
$ipaddress = $_POST['ipaddress']; 

$count = count($hostname); 

/* For each record passed, hostname cannot be null. So we run a check based on the hostname first*/

for ($i=0;$i<$count;$i++) {
	$dns = new DNSRecord();
	if(!empty($hostname)) {
		/* see what type of record this is. */
    $rtypes=$rtype[$i];
		switch($rtypes) {
			case MX:
				#echo $hostname[$i] .  " is a MX Record <BR>"; 
				if(empty($priority[$i])) {
					$priority[$i] = "200"; 
				}

				/* 
				If we're an MX record, then we need to follow some basic rules. 
				1. make sure the MX host is either an A record already in the database
				2. The MX host is a resolvable ip address. 
				3. If the user just entered the first part of the hostname, assume they mean:
				   hostname.FQDN
				4. Otherwise try to resolve the ip address. 
				
				The order to check is the following:
				
				1. Check to see if the hostname is resolvable by itself.
				2. Check to see if the hostname is already an A or CNAME record for the domain, but dns hasn't picked up yet
				
				If both fail, then throw a fatal error. 
				
				*/
  
				
	  		$dns->mSetId($fqdnid); 
	  		$dns->setMXRecord($ipaddress[$i]);
				#$dns->mSetIp($hostname[$i],$type="MX"); 
				$dns->mSetHostName($hostname[$i]);
				$dns->mSetPriority($priority[$i]);
				$dns->mSetType($rtypes);
				
      	break;
			case NS:
				echo "$hostname[$i] is a NS record <BR>"; 
				$priority = ""; 
				$dns->mSetId($fqdnid); 
				$dns->mSetIp($ipaddress[$i]); 
				$dns->mSetHostName($hostname[$i]);
				$dns->mSetPriority(0); // These records have no need for PRIORITY
				$dns->mSetType($rtypes);
				break;
      case CNAME:
      	echo "$hostname[$i] is a CNAME record <BR>"; 
				$priority = ""; 
				$type = "CNAME"; 
        $dns->mSetId($fqdnid); 
				$dns->mSetIp($ipaddress[$i],$type); 
				$dns->mSetHostName($hostname[$i]);
				$dns->mSetPriority(0); // These records have no need for PRIORITY 
				$dns->mSetType($rtypes);
        break ; 
			default: 
				echo "Adding an $rtypes Type Record <BR>"; 
				$dns->mSetId($fqdnid); 
				$dns->mSetIp($ipaddress[$i]); 
				$dns->mSetHostName($hostname[$i]);
				$dns->mSetPriority(0); // These records have no need for PRIORITY
				$dns->mSetType($rtypes);
				break;
		}
		#echo "Adding $hostname[$i] with an IP address of $ipaddress[$i]  "; 			
		$dns->mInsert();
		#echo "....Successful <BR> <center> <hr width=\"30\"> </center> "; 
		header("Location: manage.php"); 

	}
	
}
?>
